package com.fnzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fnzx.dao.CooperativeUserDao;
import com.fnzx.entity.*;
import com.fnzx.entity.dto.CoopPersonalRelationDto;
import com.fnzx.entity.vo.CooperativeWithPersonalVo;
import com.fnzx.entity.vo.UserInfoVo;
import com.fnzx.service.CooperativePersonalRelationService;
import com.fnzx.service.CooperativeUserService;
import com.fnzx.service.PersonalUserService;
import com.fnzx.service.UserService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.Query;
import com.fnzx.util.common.Md5Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("cooperativeUserService")
public class CooperativeUserServiceImpl extends ServiceImpl<CooperativeUserDao, CooperativeUserEntity> implements CooperativeUserService {
    @Autowired
    CooperativePersonalRelationService relationService;

    @Autowired
    PersonalUserService personalUserService;

    @Autowired
    UserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CooperativeUserEntity> page = this.page(
                new Query<CooperativeUserEntity>().getPage(params),
                new QueryWrapper<CooperativeUserEntity>()
        );
        List<UserInfoVo> collect = page.getRecords().stream().map(record -> {
            return userService.getUserInfoById(record.getId());
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return pageUtils;
    }

    @Override
    public CooperativeWithPersonalVo listWithPersonal(Long id) {
        //从关系表中查询属于这个合作伙伴的 个人用户的id
        List<CooperativePersonalRelationEntity> relationEntities = relationService.list(
                new QueryWrapper<CooperativePersonalRelationEntity>().eq("cooperative_id", id));
        List<Long> personalIds = relationEntities.stream().map(
                CooperativePersonalRelationEntity::getPersonalId
        ).collect(Collectors.toList());
        //子查询 查询所有个人用户
        List<UserInfoVo> collect = personalIds.stream().map(personId -> {
            return userService.getUserInfoById(personId);
        }).collect(Collectors.toList());
        //准备返回vo
        CooperativeWithPersonalVo result = new CooperativeWithPersonalVo();
        result.setPersonalUserEntities(collect);
        CooperativeUserEntity cooperativeUserEntity = this.getById(id);
        BeanUtils.copyProperties(cooperativeUserEntity,result);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addPersonInCooper(CoopPersonalRelationDto relationDto) {
        Long cooperativeUserId=relationDto.getCooperativeUserId();
        //先取出用户表的信息存进用户表，再得到用户id
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(relationDto,userEntity);
        userEntity.setUserType(12);
        userEntity.setPassword(Md5Util.md5(userEntity.getPassword()));
        //得到用户id后再存个人顾问表和关系表
        userService.save(userEntity);
        if (userEntity.getId()!=null) {
            PersonalUserEntity personalUser = relationDto.getPersonalUserDetails();
            personalUser.setId(userEntity.getId());
            personalUserService.save(personalUser);
            //存关系
            CooperativePersonalRelationEntity relationEntity = new CooperativePersonalRelationEntity();
            relationEntity.setPersonalId(userEntity.getId());
            relationEntity.setCooperativeId(cooperativeUserId);
            relationService.save(relationEntity);
        }
    }

    @Override
    public List<CooperativeWithPersonalVo> listAllWithPersonalUser(Map<String, Object> params) {
        PageUtils pageUtils = relationService.queryPage(params);
        List<CooperativePersonalRelationEntity> list = (List<CooperativePersonalRelationEntity>) pageUtils.getList();
        //收集个人用户和合作伙伴关系表中所有合作伙伴的id集合
        Set<Long> collect = list.stream().map(CooperativePersonalRelationEntity::getCooperativeId)
                .collect(Collectors.toSet());
        return collect.stream()
                .map(this::listWithPersonal)
                .filter(item->item.getId()!=1)
                .collect(Collectors.toList());
    }

    @Override
    public void addExistPersonInCooper(Long cooperId, Long personId) {
        CooperativePersonalRelationEntity relation = new CooperativePersonalRelationEntity();
        relation.setCooperativeId(cooperId);
        relation.setPersonalId(personId);
        relationService.save(relation);
    }

}